package yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import yunhe.biz.IProcureBiz;
import yunhe.biz.impl.ProcureBizImpl;
import yunhe.entity.Product;
import yunhe.entity.PurchasePack;
import yunhe.entity.Supplier;
import yunhe.util.GeneralUtil;
@WebServlet(name="InsertPurchasePackServlet",urlPatterns="/InsertPurchasePackServlet")
public class InsertPurchasePackServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PurchasePack purchasePack=new PurchasePack();
		purchasePack.setPurPackId(GeneralUtil.creatSeq("pp"));
		String supplierId=request.getParameter("supplierId");
		Supplier supplier=new Supplier();
		supplier.setSupId(supplierId);
		purchasePack.setSupplier(supplier);
		String proBarCode=request.getParameter("proBarCode");
		Product product=new Product();
		product.setProBarCode(proBarCode);
		purchasePack.setProduct(product);
		String proPurch=request.getParameter("proPurchPrice");
		double proPurchPrice=Double.parseDouble(proPurch);
		purchasePack.setProPurchPrice(proPurchPrice);
		String purPackType=request.getParameter("purPackType");
		purchasePack.setPurPackType(purPackType);
		String purPackPay=request.getParameter("purPackPayDate");
		int purPackPayDate=Integer.parseInt(purPackPay);
		purchasePack.setPurPackPayDate(purPackPayDate);
		String purPackSign=request.getParameter("purPackSignTime");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		Date purPackSignTime=null;
		try{
			purPackSignTime=new java.sql.Date(sdf.parse(purPackSign).getTime());
		}catch(Exception e){
			e.printStackTrace();
		}
		purchasePack.setPurPackSignTime(purPackSignTime);
		String purPack=request.getParameter("purPackTerm");
		int purPackTerm=Integer.parseInt(purPack);
		purchasePack.setPurPackTerm(purPackTerm);
		IProcureBiz ipb=new ProcureBizImpl();
		boolean flag=ipb.insertPurchasePack(purchasePack);
		PrintWriter out=response.getWriter();
		if(flag){
			out.print("<script>alert('合同创建成功');location.href='/materialMana/purchaseCompact.jsp'</script>");
		}else{
			out.print("<script>alert('合同创建成功');location.href='/materialMana/purchaseCompact.jsp'</script>");
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
